package cn.com.vrv.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.vrv.common.AbstractController;
import cn.com.vrv.common.TaskEnum;
import cn.com.vrv.entity.QueryObj;
import cn.com.vrv.entity.RecognitionTask;
import cn.com.vrv.service.RecognitionTaskService;
import cn.com.vrv.util.StringUtils;

/**
 * 任务控制器
 * 
 * @author 赵炎
 * @version [V1.00, 2017年11月21日]
 * @see [相关类/方法]
 * @since V1.00
 */
@Controller
@RequestMapping("task")
public class TaskController extends AbstractController
{
    @Autowired
    private RecognitionTaskService recognitionTaskService;
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * 
     * @param model
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("list")
    public String list(@RequestParam(value = "page", defaultValue = "0") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,Model model,QueryObj queryObj)
    {
        Page<RecognitionTask> pageTasks=recognitionTaskService.findBookCriteria(page, size, queryObj);
        model.addAttribute("page", pageTasks);
        model.addAttribute("queryObj", queryObj);
        model.addAttribute("url", "/task/list");
        return "recognition/taskList";
    }
    
    /**
     * <一句话功能简述>
     * <功能详细描述>
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    @RequestMapping("save")
    public String add(RecognitionTask task)
    {
    	task.setUpdateTime(StringUtils.getDate());
    	if(task.getTaskId()!=null)
    	{
    		this.recognitionTaskService.updateByTaskId(task);
    	}else
    	{
    		task.setCreateTime(StringUtils.getDate());
    		task.setStatus(TaskEnum.INIT);
    		 this.recognitionTaskService.save(task);
    	}
    	
       
        return "redirect:/task/list";
    }
    
    @RequestMapping("form")
    public String form(Model model,@Param("taskId") String taskId,@Param("view") String view)
    {
    	RecognitionTask recognitionTask=new RecognitionTask();
    	if(StringUtils.isNotEmpty(taskId))
    	{
    		 recognitionTask= this.recognitionTaskService.findByTaskId(taskId);
        	if(StringUtils.isNotEmpty(view))
        	{
        		model.addAttribute("view", view);
        	}
    	}
    	model.addAttribute("recognitionTask", recognitionTask);
        return "recognition/taskForm";
    }
    
    @RequestMapping("download")
    @ResponseBody
    public String download(@Param("fileName") String fileName,HttpServletResponse response)
    {
    	return  recognitionTaskService.downLoad(fileName, response);
    }
    
    @RequestMapping("delete")
    public String delete(RedirectAttributes redirectAttributes,@Param("taskId") String taskId)
    {
    	this.recognitionTaskService.deleteByTaskId(taskId);
    	redirectAttributes.addAttribute("info","删除成功" );
    	return "redirect:/task/list";
    }
    
  /*  @ResponseBody
    @RequestMapping("taskValue")
    public Map<String,Object> taskValue()
    {
    	List<RecognitionTask> tasks = this.recognitionTaskService.findAll();
    	JSONArray jsonArray=new JSONArray();
    	Map<String,Object> map=new HashMap<String,Object>();
    	map.put("code",0);
    	map.put("msg", "");
    	map.put("count", 1);
    	//map.put("data",tasks.size()>0?jsonArray.toJSONString(tasks):"");
    	map.put("data","[{'taskId':'10000','fileName':'user-0','taskType':'女','status':'城市-0','createTime':'签名-0'}]");
    	System.out.println(map.toString());
    	return map;
    }*/
    
}
